package module;

/**
 * Utility class to execute game modules.  Uses a Python interpreter to parse 
 * the game module and create a Game.  The Module must specify, at a minumum,
 * a function with the header "def makeGame(numPlayers)".  This method will 
 * create the prerequisite GameObjects for game play.  Optionally, it may
 * specify a function with the header "def initGame(game)".  This method will
 * specify a set of initial commands (e.g., deck shuffling, dealing) that 
 * the Game object should have applied to it at the start of the game.
 */
public class GameParser
{
	/**
	 * Creates a new GameParser for the provided Python game module.
	 * @param moduleName the name of the module (in the /modules directory) (without the .py extension)
	 * @throws IOException if the module file is not found
	 */
	public GameParser(String moduleName) throws java.io.IOException {
		interp = new org.python.util.PythonInterpreter();
		interp.execfile(new java.io.FileInputStream("modules/" + moduleName + "/main.py"));
	}

	/**
	 * Parses the game module and initializes the GameObjects of the Game, given the desired 
	 * minimum number of players.
	 */
	public core.Game parse(int numPlayers, java.util.Map<String, String> options) {
		interp.set("options", options);
		interp.exec("game = makeGame(" + numPlayers + ", options)");
		return (core.Game)interp.get("game", core.Game.class);
	}

	private final org.python.util.PythonInterpreter interp;
}